package xyz.xuminghai.javafx_learn.scene.shape;

import javafx.animation.Animation;
import javafx.animation.ScaleTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.Bloom;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * 2023/8/4 9:22 星期五<br/>
 * Path类表示一个简单的形状，并提供基本构建和管理几何路径所需的设施。
 *
 * @author xuMingHai
 */
public class PathApp extends Application {

    private final BorderPane root = new BorderPane();


    public static void main(String[] args) {
        Application.launch(args);
    }


    @Override
    public void init() {
        MoveTo moveTo = new MoveTo(0.0, 0.0);
        ArcTo arcTo = new ArcTo(50.0, 50.0,
                0.0, 100.0, 0.0,
                true, true);
        double hypot = Math.hypot(arcTo.getX(), arcTo.getY());
        CubicCurveTo cubicCurveTo = new CubicCurveTo(hypot, hypot,
                hypot - arcTo.getX(),
                hypot - arcTo.getY(),
                0.0, arcTo.getX() + arcTo.getRadiusX());

        Path path = getPath(moveTo, arcTo, cubicCurveTo);
        ScaleTransition scaleTransition = new ScaleTransition(Duration.millis(500.0), path);
        scaleTransition.setToX(1.5);
        scaleTransition.setToY(1.5);
        scaleTransition.setAutoReverse(true);
        scaleTransition.setCycleCount(Animation.INDEFINITE);
        scaleTransition.play();

        root.setCenter(path);
    }

    private Path getPath(MoveTo moveTo, ArcTo arcTo, CubicCurveTo cubicCurveTo) {
        Path path = new Path(moveTo, arcTo, cubicCurveTo,
                moveTo, new ArcTo(arcTo.getRadiusX(),
                arcTo.getRadiusY(), arcTo.getXAxisRotation(),
                -arcTo.getX(), arcTo.getY(), arcTo.isLargeArcFlag(), false),
                new CubicCurveTo(-cubicCurveTo.getControlX1(), cubicCurveTo.getControlY1(),
                        -cubicCurveTo.getControlX2(), cubicCurveTo.getControlY2(),
                        -cubicCurveTo.getX(), cubicCurveTo.getY()));

        path.setFill(Color.RED);
        DropShadow dropShadow = new DropShadow();
        Bloom bloom = new Bloom();
        bloom.setInput(dropShadow);
        path.setEffect(bloom);
        return path;
    }

    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
